package me.zhengjie.timing.task;

import lombok.RequiredArgsConstructor;
import me.zhengjie.agvSend.AgvSendService;
import me.zhengjie.pojo.dto.NoticeAgvDto;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.sqlservice.*;
import me.zhengjie.tcp.ChainLineTCP;
import me.zhengjie.tcp.FluentFrame;
import me.zhengjie.utility.state.MaterialStatus;
import me.zhengjie.utility.state.TaskType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@RequiredArgsConstructor
public class AgvReleaseGoodsTiming {
    private static final Logger fluentFrameLog = LoggerFactory.getLogger("fluentFrame");
    private final WcsAgvReleaseGoodsService wcsAgvReleaseGoodsService;

    private final WcsOutboundMissionService wcsOutboundMissionService;

    private final AgvSendService agvSendService;

    private final WcsFluentFrameConfigService wcsFluentFrameConfigService;

//    private final FluentFrameTCPA fluentFrameTCPA;
//
//    private final FluentFrameTCPB fluentFrameTCPB;
    private final FluentFrame fluentFrame;

    private final WcsInboundMissionService wcsInboundMissionService;

    private final WcsChainLineConfigService wcsChainLineConfigService;

    private final ChainLineTCP chainLineTCP;

    @Scheduled(fixedDelay = 1000)
    @Transactional
    public void agvReleaseGoodsTiming() {
        List<WcsAgvReleaseGoods> wcsAgvReleaseGoodsList = wcsAgvReleaseGoodsService.selectFloor(TaskType.TWO_FLOOR.getStatus(), 1);
        List<WcsAgvReleaseGoods> oneWcsAgvReleaseGoodsList = wcsAgvReleaseGoodsService.selectFloor(TaskType.ONE_FLOOR.getStatus(), 1);
        if (!wcsAgvReleaseGoodsList.isEmpty()) {
            for (WcsAgvReleaseGoods wcsAgvReleaseGoods : wcsAgvReleaseGoodsList) {
                String taskCode = wcsAgvReleaseGoods.getTaskCode();
                Integer id = wcsAgvReleaseGoods.getId();
                WcsInboundMission wcsInboundMission = wcsInboundMissionService.selectInboundCode(taskCode);
                WcsOutboundMission wcsOutboundMission = wcsOutboundMissionService.selectOutboundCode(taskCode);
                if (wcsInboundMission !=null){
//                    //更具2楼入库起点去找琉璃架
                    String source = wcsInboundMission.getSource();
                    WcsFluentFrameConfig wcsFluentFrameConfig = wcsFluentFrameConfigService.selectInboundStorageCode(source);
                    if (wcsFluentFrameConfig != null) {
                        String region = wcsFluentFrameConfig.getRegion();
                            Integer inbRequestTake = wcsFluentFrameConfig.getInbRequestTake();
                            Integer unitId = wcsFluentFrameConfig.getUnitId();
                            Integer threeInbReturnResult = wcsFluentFrameConfig.getThreeInbReturnResult();
                            String inbAgvEqptCode = wcsFluentFrameConfig.getInbAgvEqptCode();
                            //查询PLC是否允许取货
                            Integer inbRequestTakeStatus = fluentFrame.readHoldingRegisters(region,inbRequestTake, unitId);
                            if (inbRequestTakeStatus != null && inbRequestTakeStatus == 1) {
                                //返回PLC放货中
                                boolean b1 = fluentFrame.writeWithRetry(threeInbReturnResult, MaterialStatus.AGV_TRANSPORT_IN_THE_TASK.getStatus(), unitId, MaterialStatus.RETRY_COUNT.getStatus(),region);
                                fluentFrameLog.info("流利架A入库地址位：{}写入返回结果：{}",threeInbReturnResult,b1);
                                if (!b1){
                                    continue;
                                }
                                NoticeAgvDto noticeAgvDto = new NoticeAgvDto();
                                noticeAgvDto.setTaskCode(taskCode);
                                noticeAgvDto.setActionStatus("5");
                                noticeAgvDto.setEqptCode(inbAgvEqptCode);
                                boolean b = agvSendService.noticeAgv(noticeAgvDto);
                                if (b){
                                    //删除AGV取放货表
                                    wcsAgvReleaseGoodsService.delete(id);
                                }
                            }
                    }
                }else if (wcsOutboundMission !=null){
//                    String destination = wcsOutboundMission.getDestination();
                    String destination = wcsOutboundMission.getFluentFrameStorage();
                    WcsFluentFrameConfig wcsFluentFrameConfig = wcsFluentFrameConfigService.selectOutStorageCode(destination);
                    if (wcsFluentFrameConfig !=null){
                        String region = wcsFluentFrameConfig.getRegion();
                            Integer outRequestRelease = wcsFluentFrameConfig.getOutRequestRelease();
                            Integer unitId = wcsFluentFrameConfig.getUnitId();
                            Integer threeOutReturnResult = wcsFluentFrameConfig.getThreeOutReturnResult();
                            String outAgvEqptCode = wcsFluentFrameConfig.getOutAgvEqptCode();
                            //查询PLC是否允许取货
                            Integer inbRequestTakeStatus = fluentFrame.readHoldingRegisters(region,outRequestRelease, unitId);
                            if (inbRequestTakeStatus != null && inbRequestTakeStatus == 1) {
                                boolean b1 = fluentFrame.writeWithRetry(threeOutReturnResult, MaterialStatus.AGV_TRANSPORT_IN_THE_TASK.getStatus(), unitId, MaterialStatus.RETRY_COUNT.getStatus(),region);
                                fluentFrameLog.info("流利架A出库地址位：{}写入返回结果：{}",threeOutReturnResult,b1);
                                if (!b1){
                                    continue;
                                }
                                NoticeAgvDto noticeAgvDto = new NoticeAgvDto();
                                noticeAgvDto.setTaskCode(taskCode);
                                noticeAgvDto.setActionStatus("6");
                                noticeAgvDto.setEqptCode(outAgvEqptCode);
                                boolean b = agvSendService.noticeAgv(noticeAgvDto);
                                if (b){
                                    //删除AGV取放货表
                                    wcsAgvReleaseGoodsService.delete(id);
                                }
                            }
                    }
                }
            }
        }
        if (!oneWcsAgvReleaseGoodsList.isEmpty()){
            for (WcsAgvReleaseGoods wcsAgvReleaseGoods : oneWcsAgvReleaseGoodsList) {
                String taskCode = wcsAgvReleaseGoods.getTaskCode();
                Integer id = wcsAgvReleaseGoods.getId();
                WcsInboundMission wcsInboundMission = wcsInboundMissionService.selectInboundCode(taskCode);
                if (wcsInboundMission !=null){
                    String source = wcsInboundMission.getSource();
                    //查询1F链条机
                    WcsChainLineConfig wcsChainLineConfig = wcsChainLineConfigService.selectTransportStorage(source);
                    if (wcsChainLineConfig != null){
                        Integer threeReturnResult = wcsChainLineConfig.getThreeReturnResult();
                        Integer unitId = wcsChainLineConfig.getUnitId();
                        String eqptCode = wcsChainLineConfig.getEqptCode();
                        //向plc写入AGV执行中
                        boolean b = chainLineTCP.writeWithRetry(threeReturnResult, MaterialStatus.AGV_TRANSPORT_IN_THE_TASK.getStatus(), unitId, MaterialStatus.RETRY_COUNT.getStatus());
                        if (!b){
                            continue;
                        }
                        NoticeAgvDto noticeAgvDto = new NoticeAgvDto();
                        noticeAgvDto.setTaskCode(taskCode);
                        noticeAgvDto.setActionStatus("5");
                        noticeAgvDto.setEqptCode(eqptCode);
                        boolean b1 = agvSendService.noticeAgv(noticeAgvDto);
                        if (b1){
                            //删除AGV取放货表
                            wcsAgvReleaseGoodsService.delete(id);
                        }
                    }
                }

            }
        }

    }
}
